using System;
using System.Collections;
using System.Collections.Generic;
using Server.Mobiles;
using Server.Items;
using Server.Gumps;
using Server.Network;
using Server.Engines.BulkOrders;


namespace Server.Gumps
{
	public class CommVendorBuyGump : Gump
	{
		private Mobile m_vendor;
		private TradeItem m_ti;

		public CommVendorBuyGump( Mobile vendor, TradeItem ti ) : base( 100, 200 )
		{
			m_vendor = vendor;
			m_ti = ti;

			Item item = ti.ItemObj as Item;
			IHardsellable hs = item.RootParent as IHardsellable;

			AddBackground( 100, 10, 300, 180, 5054 );

			AddHtmlLocalized( 125, 20, 250, 24, 1019070, false, false ); // You have agreed to purchase:

			if ( !String.IsNullOrEmpty( ti.Description ) )
				AddLabel( 125, 45, 0, ti.Description );
			else
				AddHtmlLocalized( 125, 45, 250, 24, 1019072, false, false ); // an item without a description

			AddHtmlLocalized( 125, 70, 250, 24, 1019071, false, false ); // for the amount of:
			AddLabel( 125, 95, 0, ti.Price.ToString() );

			AddButton( 250, 130, 4005, 4007, 0, GumpButtonType.Reply, 0 );
			AddHtmlLocalized( 282, 130, 100, 24, 1011012, false, false ); // CANCEL

			AddButton( 120, 130, 4005, 4007, 1, GumpButtonType.Reply, 0 );
			AddHtmlLocalized( 152, 130, 100, 24, 1011036, false, false ); // OKAY


			if ( hs != null )
			{
				int x = 125;
				string name = "Currency: " + hs.Currency.Name;

				CurrencyInfo cinfo = hs.CInfo;

				int itemID = cinfo.m_itemID;
				int hue = cinfo.m_hue;
				bool dupeicon = cinfo.m_dupeicon;
	
				int xloc = x + name.Length * 6;

				AddLabel( x, 162, 0x8AB, name );			// AddLabel( xloc, y+56, 0x8AB, name );
				AddItem( xloc, 160, itemID, hue );				// AddItem( x - iconWidth, y+54, coin, 0 );

				if ( dupeicon )
					AddItem( xloc + 5, 165, itemID, hue );
			}

		}

		public override void OnResponse( NetState state, RelayInfo info )
		{
			Mobile from = state.Mobile;

			if ( !VendorManager.CanTradeWith( from, m_vendor ) )
				return;

			if ( info.ButtonID == 1 )
			{
				IHardsellable hs = m_vendor as IHardsellable;
				Item item = (Item)m_ti.ItemObj;

				if ( hs.HoldBox == null || m_ti == null )
				{
					from.SendMessage( "Error: vendor box null, or item null." );
					return;
				}

				if ( hs.IsOwner( from ) )
				{
					m_vendor.SayTo( from, 503212 ); // You own this shop, just take what you want.
				}

				List<Item> list = hs.HoldBox.Items;
				bool found = false;

				for ( int i = 0; i < list.Count; i++ )
				{
					if ( list[i] == item )
						found = true;
				}

				// containers may be in show box
				if ( item is Container || item is BulkOrderBook )
				{
					list = hs.ShowBox.Items;

					for ( int i = 0; i < list.Count; i++ )
					{
						if ( list[i] == item )
							found = true;
					}
				}

				if ( !found )
				{
					from.SendMessage( "Error: item not found." );
					return;
				}

				VendorManager.CurrencyBuyItem( from, m_ti );
			}
			else
			{
				from.SendLocalizedMessage( 503207 ); // Cancelled purchase.
			}
		}
	}

	public class CommItemTileButtonInfo : CommImageTileButtonInfo
	{
		private Item m_Item;

		public Item Item
		{
			get { return m_Item; }
			set { m_Item = value; }
		}

		public CommItemTileButtonInfo( Item i ) : base( i.ItemID, i.Hue, ((i.Name == null || i.Name.Length <= 0)? (TextDefinition)i.LabelNumber : (TextDefinition)i.Name ) )
		{
			m_Item = i;
		}
	}

	public class TradeItemButtonInfo : CommImageTileButtonInfo
	{
		private Item m_Item;

		public Item Item
		{
			get { return m_Item; }
			set { m_Item = value; }
		}

		public TradeItemButtonInfo( TradeItem ti ) : this( ti, -1 )
		{
		}

		public TradeItemButtonInfo( TradeItem ti, int qty ) : base( ((Item)ti.ItemObj).ItemID, ((Item)ti.ItemObj).Hue, GetTextLabel( ti, qty ) )
		{
			m_Item = ti.ItemObj as Item;
		}

		private static TextDefinition GetTextLabel( TradeItem i, int qty )
		{
			Item item = i.ItemObj as Item;
			string name = i.Description;
			string sqty = "";

			if ( name == null || name.Length <= 0 )
				name = VendorManager.ToGetName( item, false );

			if ( name != null && name.Length > 23 )
				name = name.Substring( 0, 23 );		// max length of a name is 23 characters

			if ( name == null || name.Length <= 0 )
				name = "a item";

			if ( qty >= 0 )
				sqty = "Quantity : " + qty.ToString();

			if ( i.IsForFree )
				name = name + "<br>" + sqty + "<br>" + "FREE!";
			else if ( i.IsForSale )
				name = name + "<br>" + sqty + "<br>" + i.Price.ToString();
			else if ( i.IsForDemo )
				name = name + "<br>" + sqty + "<br>" + "Demo!";

			// always name thou
			TextDefinition label = ( ( name == null || name.Length <= 0 ) 
				? (TextDefinition)item.LabelNumber : (TextDefinition)name );

			return label;
		}
	}

	public class HardItemDisplayGump : CommImageTileButtonsGump
	{
		public static ArrayList CreateVendorSalesItems( Mobile m, Mobile vendor, IHardsellable hs )
		{
			if ( hs.ShopItems == null || !( hs.ShopItems.Count > 0 ) )
				return new ArrayList();

			ArrayList buttons = new ArrayList();
			int qty = -1;

			foreach ( TradeItem ti in hs.ShopItems.Values )
			{
				if ( vendor is RestockableVendor )
				{
					RestockableVendor rv = vendor as RestockableVendor;

					qty = VendorManager.GetBuyItemQuantity( rv, ti, false );
				}

				buttons.Add( new TradeItemButtonInfo( ti, qty ) );
			}

			return buttons;
		}

		Mobile m_buyer, m_vendor;

		public HardItemDisplayGump( Mobile buyer, Mobile vendor, ArrayList buttons ) : base( "Sales Items", buttons, vendor )
		{
			m_buyer = buyer;
			m_vendor = vendor;
		}

		public HardItemDisplayGump( Mobile buyer, Mobile vendor, CommItemTileButtonInfo[] buttons ) : base( "Sales Items", buttons, vendor )
		{
			m_buyer = buyer;
			m_vendor = vendor;
		}

		public override void HandleButtonResponse( NetState sender, int adjustedButton, CommImageTileButtonInfo buttonInfo )
		{
			if ( m_vendor == null || m_vendor.Deleted || !( m_vendor is IHardsellable ) )
			{
				sender.Mobile.SendMessage( "Buyer is not the same person." );
				return;
			}

			Mobile m = sender.Mobile;
			Item item = ((TradeItemButtonInfo)buttonInfo).Item;
			IHardsellable hs = m_vendor as IHardsellable;

			if ( hs.HoldBox == null || hs.ShopItems == null || !( hs.ShopItems.Count > 0 ) )
			{
				m_buyer.SendMessage( "Vendor has no item for sale." );
				return;
			}

			List<Item> list = hs.HoldBox.Items;
			List<Item> demos = hs.ShowBox.Items;

			if ( demos != null && demos.Count > 0 )
			{
				for ( int i = 0; i < demos.Count; i++ )
				{
					Item demo = demos[i];

					if ( !( demo is CloneItem ) )
					{
						demo.Internalize();
						hs.HoldBox.DropItem( demo );
					}
				}
			}

			bool found = false;

			// can be switched to show items as CloneItem or true item,
			// admins and GMs will feel odd if true items are displayed
			// as item.Visible=false inside containers doesn't work for them
			bool debugMode = false;

			for ( int i = 0; i < list.Count; i++ )
			{
				if ( item == list[i] )
					found = true;
			}


			if ( found )
			{
				if ( debugMode && m.AccessLevel > AccessLevel.Player )
				{
					Timer.DelayCall( TimeSpan.Zero, new TimerStateCallback( DisplayItemCallback ), new object[] { m, hs.HoldBox } );
				}

				m_vendor.RemoveItem( hs.ShowBox );
				hs.ShowBox = new VendorBox();
				m_vendor.AddItem( hs.ShowBox );

				Item demo = new Item();

				if ( item is Container || item is BulkOrderBook )
				{
					demo = item;
					item.Visible = true;
				}
				else
				{
					demo = new CloneItem( m, item );
					demo.Internalize();

					demo.InvalidateProperties();
					demo.Movable = false;
				}

				VendorManager.PlaceItemIn( hs.ShowBox, 72, 128, demo );
				Timer.DelayCall( TimeSpan.Zero, new TimerStateCallback( DisplayItemCallback ), new object[] { m, hs.ShowBox } );
			}

			ArrayList buttons = HardItemDisplayGump.CreateVendorSalesItems( m, (Mobile)m_vendor, hs );

			if( buttons.Count > 0 )
			{
				m_buyer.CloseGump( typeof( HardItemDisplayGump ) );
				m_buyer.SendGump( new HardItemDisplayGump( m_buyer, m_vendor, buttons ) );
			}

		}

		public override void HandleCancel( NetState sender )
		{
			Mobile m = sender.Mobile;

		}

		private void DisplayItemCallback( object state )
		{
			object[] aState = (object[]) state;

			Mobile from = (Mobile) aState[0];
			Container cont = (Container) aState[1];

			cont.DisplayTo( from );
		}
	}

	public class BidItemDisplayGump : CommImageTileButtonsGump
	{
		public static ArrayList CreateVendorBidItems( Mobile m, Mobile vendor )
		{
			ArrayList buttons = new ArrayList();

			IHardsellable hs = vendor as IHardsellable;

			if ( hs == null || hs.BidItems == null || !( hs.BidItems.Count > 0 ) )
				return new ArrayList();

			for ( int i = 0; i < hs.BidItems.Count; i++ )
			{
				Item item = (Item)hs.BidItems[i].m_itemobj;
				TradeItem ti = VendorManager.GetTradeItem( hs, item );

				buttons.Add( new TradeItemButtonInfo( ti, -1 ) );
			}

			return buttons;
		}

		Mobile m_buyer, m_vendor;

		public BidItemDisplayGump( Mobile buyer, Mobile vendor, ArrayList buttons ) : base( "Bid Items", buttons, vendor )
		{
			m_buyer = buyer;
			m_vendor = vendor;
		}

		public BidItemDisplayGump( Mobile buyer, Mobile vendor, CommItemTileButtonInfo[] buttons ) : base( "Bid Items", buttons, vendor )
		{
			m_buyer = buyer;
			m_vendor = vendor;
		}

		public override void HandleButtonResponse( NetState sender, int adjustedButton, CommImageTileButtonInfo buttonInfo )
		{
			if ( m_vendor == null || m_vendor.Deleted || !( m_vendor is IHardsellable ) )
				return;

			Mobile m = sender.Mobile;
			Item item = ((TradeItemButtonInfo)buttonInfo).Item;
			IHardsellable hs = m_vendor as IHardsellable;

			if ( hs.BidItems == null || !( hs.BidItems.Count > 0 ) )
			{
				m_buyer.SendMessage( "Vendor receives no bid yet." );
				return;
			}

			List<Item> list = hs.HoldBox.Items;
			List<Item> demos = hs.ShowBox.Items;

			if ( demos != null && demos.Count > 0 )
			{
				for ( int i = 0; i < demos.Count; i++ )
				{
					Item demo = demos[i];

					if ( !( demo is CloneItem ) )
					{
						demo.Internalize();
						hs.HoldBox.DropItem( demo );
					}
				}
			}

			bool found = false;
			bool debugMode = false;

			for ( int i = 0; i < list.Count; i++ )
			{
				if ( item == list[i] )
					found = true;
			}

			if ( found )
			{
				if ( debugMode && m.AccessLevel > AccessLevel.Player )
				{
					Timer.DelayCall( TimeSpan.Zero, new TimerStateCallback( DisplayItemCallback ), new object[] { m, hs.HoldBox } );
				}

				m_vendor.RemoveItem( hs.ShowBox );
				hs.ShowBox = new VendorBox();
				m_vendor.AddItem( hs.ShowBox );

				Item demo = new Item();

				if ( item is Container || item is BulkOrderBook )
				{
					demo = item;
					item.Visible = true;
				}
				else
				{
					demo = new CloneItem( m, item );
					demo.Internalize();

					demo.InvalidateProperties();
					demo.Movable = false;
				}

				VendorManager.PlaceItemIn( hs.ShowBox, 72, 128, demo );
				Timer.DelayCall( TimeSpan.Zero, new TimerStateCallback( DisplayItemCallback ), new object[] { m, hs.ShowBox } );
			}

			ArrayList buttons = BidItemDisplayGump.CreateVendorBidItems( m, (Mobile)m_vendor );

			if( buttons.Count > 0 )
			{
				m_buyer.CloseGump( typeof( BidItemDisplayGump ) );
				m_buyer.SendGump( new BidItemDisplayGump( m_buyer, m_vendor, buttons ) );
			}

		}

		public override void HandleCancel( NetState sender )
		{
			Mobile m = sender.Mobile;

		}

		private void DisplayItemCallback( object state )
		{
			object[] aState = (object[]) state;

			Mobile from = (Mobile) aState[0];
			Container cont = (Container) aState[1];

			cont.DisplayTo( from );
		}

	}

	public class TileDisplayGump : BaseImageTileButtonsGump
	{
		public static ArrayList CreateTileItems( Mobile m )
		{
			ArrayList buttons = new ArrayList();

			Item item = new CommVendorTile();		// default tile
			buttons.Add( new ItemTileButtonInfo( item ) );

			for ( int i = 0; i < VendorManager.MallTiles.Length; i++ )
			{
				int itemID = VendorManager.MallTiles[i];
				item = new CommVendorTile( itemID, 0 );

				buttons.Add( new ItemTileButtonInfo( item ) );
			}

			return buttons;
		}

		Mobile m_Manager;

		public TileDisplayGump( Mobile manager, ArrayList buttons ) : base( "Select Tile", buttons ) // Click a minor artifact to give it to Ihara Soko.
		{
			m_Manager = manager;
		}

		public TileDisplayGump( Mobile manager, ItemTileButtonInfo[] buttons ) : base( "Select Tile", buttons ) // Click a minor artifact to give it to Ihara Soko.
		{
			m_Manager = manager;
		}

		public override void HandleButtonResponse( NetState sender, int adjustedButton, ImageTileButtonInfo buttonInfo )
		{
			Mobile from = sender.Mobile;
			Item item = ((ItemTileButtonInfo)buttonInfo).Item;

			if ( m_Manager != null && !m_Manager.Deleted )
			{
				((VendorManager)m_Manager).TileID = item.ItemID;
				((VendorManager)m_Manager).TileHue = item.Hue;
			}

			from.CloseGump( typeof( TileSetupGump ) );
			from.SendGump( new TileSetupGump( (VendorManager)m_Manager ) );
		}

		public override void HandleCancel( NetState sender )
		{
			Mobile from = sender.Mobile;

			from.CloseGump( typeof( TileSetupGump ) );
			from.SendGump( new TileSetupGump( (VendorManager)m_Manager ) );
		}
	}
}